<?php
import("@.Action.CommonAction");
class IndexAction extends Common {
    public function index(){

		$TableName = $_GET['TableName'];

		if($_SESSION['UserName'] == 'sa'){

			$TableName = "Tables";

			$Model = new Model(); // 实例化一个model对象 没有对应任何数据表

			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   TableFields.Field ");
			$sqlB->Append(" , TableFields.Alias ");
			$sqlB->Append(" , TableFields.RefTable ");
			$sqlB->Append(" , TableFields.RefField ");
			$sqlB->Append(" , TableFields.RefFieldDisplay ");
			$sqlB->Append("FROM Tables ");
			$sqlB->Append("INNER JOIN TableFields ON `Tables`.Table = TableFields.Table ");
			$sqlB->Append("   AND `Tables`.`Table` = '" .$TableName. "' ");
			$sqlB->Append("   AND `TableFields`.`DisplayInParent` = 1 ");
			$sqlB->Append("ORDER BY TableFields.`Sequence` ");
			$sqlB->Append(";");

			$TableFieldsInfo = $Model->query($sqlB->ToString());
			
			//Debug($TableFieldsInfo);
			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   `Tables`.`Key` ");
			$sqlB->Append(" , `Tables`.Alias ");
			$sqlB->Append(" , `Tables`.`Table` ");
			$sqlB->Append("FROM `Tables` ");
			$sqlB->Append("WHERE `Tables`.`Table` = '" .$TableName. "' ");
			$sqlB->Append("Limit 1");
			$sqlB->Append(";");

			$TableInfo = $Model->query($sqlB->ToString());

			
			$sqlB = new StringBuilder();

			$sqlB->Append("SELECT ");
			
			$RefTable_InnerJoin_Arr;
			$RefIndex= 0;

			for($i = 0; $i <count($TableFieldsInfo); $i++)
			{
					if($i > 0){
						$sqlB->Append(", ");
					}
					if($TableFieldsInfo[$i]['RefTable'] != ""){

						$sqlB->Append("   " . $TableFieldsInfo[$i]['RefTable'] . ".`" . $TableFieldsInfo[$i]['RefFieldDisplay']. "` AS  " . $TableFieldsInfo[$i]['Field'] );
						$RefTable_InnerJoin_Arr[$RefIndex]['RefTable'] = $TableFieldsInfo[$i]['RefTable'];
						$RefTable_InnerJoin_Arr[$RefIndex]['Field'] = $TableFieldsInfo[$i]['Field'];
						$RefTable_InnerJoin_Arr[$RefIndex]['RefField'] = $TableFieldsInfo[$i]['RefField'];
						$RefIndex += 1;

					}else
					{
						$sqlB->Append("   TableName.`".$TableFieldsInfo[$i]['Field']. "` ");
					}
					
			}
		
			$sqlB->Append("FROM  `" .$TableName. "` AS TableName ");

			for($j = 0; $j <count($RefTable_InnerJoin_Arr); $j++)
			{
				$sqlB->Append("INNER JOIN  `" . $RefTable_InnerJoin_Arr[$j]['RefTable'] . "` ON" );
				$sqlB->Append("`TableName`.`" . $RefTable_InnerJoin_Arr[$j]['Field'] . "` = `" . $RefTable_InnerJoin_Arr[$j]['RefTable']. "`.`" . $RefTable_InnerJoin_Arr[$j]['RefField'] . "`");
			}

			if($KeyID != ""){
				$sqlB->Append("WHERE `TableName`.`" . $TableInfo[0]['Key'] . "` = '" . $KeyID . "' ");
			}

			$sqlB->Append(";");


			$TableList = $Model->query($sqlB->ToString());

			$this->assign('TableInfo',$TableInfo);
			$this->assign('TableFieldsInfo',$TableFieldsInfo);
			$this->assign('TableList',$TableList);
			$this->display("Index");
		}
		else{
		
			$this->redirect("/ImportSheet/ViewParentsProduct");
		}
    }
}